<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E5%9B%9B-rdb-aof%E6%B7%B7%E5%90%88%E6%8C%81%E4%B9%85%E5%8C%96/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">752 字 ~4分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h1 id="redis4-0新特性-四-rdb-aof混合持久化">redis4.0新特性(四)-RDB-AOF混合持久化</h1>

<p><strong>简介：</strong> redis有两种持久化的方式——RDB和AOF，RDB是一份内存快照，AOF则为可回放的命令日志，他们两个各有特点也相互独立。4.0开始允许使用RDB-AOF混合持久化的方式，结合了两者的优点，通过<code>aof-use-rdb-preamble</code>配置项可以打开混合开关，yes则表示开启，no表示禁用，默认是禁用的，可通过config set修改。</p>

<h2 id="rdb-v-s-aof">RDB V.S. AOF</h2>

<h3 id="1-rdb">1. RDB</h3>

<p>RDB文件本质上是一份内存快照，保存了创建RDB文件那个时间点的redis全量数据。具有数据文件小创建、恢复快的优点，但是由于快照的特性无法保存创建RDB之后的增量数据。</p>

<h3 id="2-aof">2. AOF</h3>

<p>AOF文件本质上是一份执行日志保存所有对redis进行更改的命令，增量数据也就随命令写入AOF文件刷盘的策略，由配置项appendfsync控制可以选择&rdquo;everysec&rdquo;或&rdquo;always&rdquo;。</p>

<p>AOF文件基本上是human-readable的文本，所以其体积相对较大，在从AOF文件恢复数据时就是做日志回放执行AOF文件中记录的所有命令，所以相对RDB而言恢复耗时较长。</p>

<p>随着redis的运行AOF文件会不断膨胀，由aofrewrite机制来防止磁盘空间被撑满详见上一篇文章<a href="https://yq.aliyun.com/articles/177819?spm=5176.100239.blogcont193034.16.bJvNNC">《redis4.0之利用管道优化aofrewrite》</a>。</p>

<h2 id="rdb-aof混合持久化">RDB-AOF混合持久化</h2>

<p>细细想来，aofrewrite时也是先写一份全量数据到新AOF文件中再追加增量，只不过全量数据是以redis命令的格式写入。那么是否可以先以RDB格式写入全量数据，再追加增量日志呢？这样既可以提高aofrewrite和恢复速度，也可以减少文件大小，还可以保证数据的完毕性，整合RDB和AOF的优点。那么现在4.0实现了这一特性——RDB-AOF混合持久化。</p>

<h3 id="aofrewrite">aofrewrite</h3>

<p>综上所述RDB-AOF混合持久化体现在aofrewrite时，那么我们就从这里开始来看4.0是如何实现的。</p>

<ul>
<li><p>回忆下aofrewrite的过程</p>

<p>无论是serverCron触发或者执行BGREWRITEAOF命令，最终redis都会走到rewriteAppendOnlyFileBackground()，</p>

<p>rewriteAppendOnlyFileBackground函数会fork子进程子进程进入rewriteAppendOnlyFile函数来生成新的AOF文件混合持久化就从这里开始。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C" data-lang="C"><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">rewriteAppendOnlyFile</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">filename</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">aof_use_rdb_preamble</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">error</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveRio</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">aof</span><span style="color:#000;font-weight:bold">,</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">error</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">RDB_SAVE_AOF_PREAMBLE</span><span style="color:#000;font-weight:bold">,</span><span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">C_ERR</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">errno</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">error</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rewriteAppendOnlyFileRio</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">aof</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">C_ERR</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div></li>

<li><p>可以看到当混合持久化开关打开时，就会进入rdbSaveRio函数先以RDB格式来保存全量数据。 前文说道子进程在做aofrewrite时，会通过管道从父进程读取增量数据并缓存下来，那么在以RDB格式保存全量数据时也会从管道读取数据，并不会造成管道阻塞。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C" data-lang="C"><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">rdbSaveRio</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rio</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">error</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">flags</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">rdbSaveInfo</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">rsi</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span>
    <span style="color:#000">snprintf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">magic</span><span style="color:#000;font-weight:bold">,</span><span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">magic</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">REDIS%04d</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">RDB_VERSION</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbWriteRaw</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">magic</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">9</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveInfoAuxFields</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">flags</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">rsi</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span></code></pre></div></li>

<li><p>首先把RDB的版本（注意不是redis的版本）和辅助域写入文件</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C" data-lang="C">    <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">dbnum</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">j</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">redisDb</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">db</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">j</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">dict</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">dict</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">dictSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">continue</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">di</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dictGetSafeIterator</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000">di</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">C_ERR</span><span style="color:#000;font-weight:bold">;</span>

        <span style="color:#8f5902;font-style:italic">/* Write the SELECT DB opcode */</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">RDB_OPCODE_SELECTDB</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveLen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">j</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>

        <span style="color:#8f5902;font-style:italic">/* Write the RESIZE DB opcode. We trim the size to UINT32_MAX, which
</span><span style="color:#8f5902;font-style:italic">        * is currently the largest type we are able to represent in RDB sizes.
</span><span style="color:#8f5902;font-style:italic">        * However this does not limit the actual size of the DB to load since
</span><span style="color:#8f5902;font-style:italic">        * these sizes are just hints to resize the hash tables. */</span>
        <span style="color:#000">uint32_t</span> <span style="color:#000">db_size</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">expires_size</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">db_size</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">dictSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">dict</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">UINT32_MAX</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">?</span>
                                <span style="color:#000">dictSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">dict</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">:</span>
                                <span style="color:#000">UINT32_MAX</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">expires_size</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">dictSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">expires</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">UINT32_MAX</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">?</span>
                                <span style="color:#000">dictSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">expires</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">:</span>
                                <span style="color:#000">UINT32_MAX</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">RDB_OPCODE_RESIZEDB</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveLen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">db_size</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveLen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">expires_size</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span></code></pre></div></li>

<li><p>然后遍历DB先把dbnum和db_size、expires_size写入文件</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C" data-lang="C">        <span style="color:#8f5902;font-style:italic">/* Iterate this DB writing every entry */</span>
        <span style="color:#204a87;font-weight:bold">while</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">de</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dictNext</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">di</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">sds</span> <span style="color:#000">keystr</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dictGetKey</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">de</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">robj</span> <span style="color:#000">key</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">o</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dictGetVal</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">de</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">expire</span><span style="color:#000;font-weight:bold">;</span>

            <span style="color:#000">initStaticStringObject</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">key</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">keystr</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">expire</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">getExpire</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">db</span><span style="color:#000;font-weight:bold">,</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">key</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveKeyValuePair</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">key</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">o</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">expire</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">now</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>

            <span style="color:#8f5902;font-style:italic">/* When this RDB is produced as part of an AOF rewrite, move
</span><span style="color:#8f5902;font-style:italic">            * accumulated diff from parent to child while rewriting in
</span><span style="color:#8f5902;font-style:italic">            * order to have a smaller final write. */</span>
            <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">RDB_SAVE_AOF_PREAMBLE</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span>
                <span style="color:#000">rdb</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">processed_bytes</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">processed</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">AOF_READ_DIFF_INTERVAL_BYTES</span><span style="color:#000;font-weight:bold">)</span>
            <span style="color:#000;font-weight:bold">{</span>
                <span style="color:#000">processed</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">rdb</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">processed_bytes</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#000">aofReadDiffFromParent</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000">dictReleaseIterator</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">di</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000">di</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* So that we don&#39;t release it again on error. */</span></code></pre></div></li>

<li><p>在当前DB中遍历所有的key把key-value对及过期时间（如果有设置的话写入文件）。这里小插曲一下在rdbSaveKeyValuePair函数中会判断expire是否已经到了过期时间如果已经过期就不会写入文件，同时如果flags标记了RDB_SAVE_AOF_PREAMBLE的话，说明是在aofrewrite且开启了RDB-AOF混合开关，此时就会从父进程去读取增量数据了。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-CPP" data-lang="CPP">    <span style="color:#8f5902;font-style:italic">/* EOF opcode */</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbSaveType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">RDB_OPCODE_EOF</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">/* CRC64 checksum. It will be zero if checksum computation is disabled, the
</span><span style="color:#8f5902;font-style:italic">    * loading code skips the check in this case. */</span>
    <span style="color:#000">cksum</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">rdb</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">cksum</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">memrev64ifbe</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">cksum</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rioWrite</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">cksum</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">werr</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">C_OK</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
</code></pre></div></li>

<li><p>最后把代表RDB格式结束的RDB_OPCODE_EOF标记和校验和写入文件。</p></li>
</ul>

<p>RDB-AOF混合持久化的RDB部分到此结束rdbSaveRio函数运行完后返回rewriteAppendOnlyFile继续把增量数据写入AOF文件。</p>

<p>也就是说AOF文件的前半段是RDB格式的全量数据后半段是redis命令格式的增量数据。</p>

<h3 id="数据恢复">数据恢复</h3>

<p>当appendonly配置项为no时，redis启动后会去加载RDB文件，以RDB格式来解析RDB文件自然没有问题。</p>

<p>而appendonly配置项为yes时，redis启动后会加载AOF文件来恢复数据，如果持久化时开启了RDB-AOF混合开关，那么AOF文件的前半段就是RDB格式，此时要如何正确加载数据呢？</p>

<p>一切数据都逃不过协议二字，不以正确的协议存储和解析那就是乱码，既然允许RDB-AOF混合持久化就要能够识别并恢复数据。这一节我们来介绍如何以正确的姿势来恢复数据。</p>

<p>加载AOF文件的入口为loadAppendOnlyFile。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-CPP" data-lang="CPP"><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">loadAppendOnlyFile</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">filename</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span>
    <span style="color:#8f5902;font-style:italic">/* Check if this AOF file has an RDB preamble. In that case we need to
</span><span style="color:#8f5902;font-style:italic">     * load the RDB file and later continue loading the AOF tail. */</span>
    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">sig</span><span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* &#34;REDIS&#34; */</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">fread</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sig</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">fp</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">5</span> <span style="color:#ce5c00;font-weight:bold">|</span><span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#000">memcmp</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sig</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">REDIS</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#8f5902;font-style:italic">/* No RDB preamble, seek back at 0 offset. */</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">fseek</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">fp</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">SEEK_SET</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">readerr</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#8f5902;font-style:italic">/* RDB preamble. Pass loading the RDB functions. */</span>
        <span style="color:#000">rio</span> <span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">;</span>

        <span style="color:#000">serverLog</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LL_NOTICE</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Reading RDB preamble from AOF file...</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">fseek</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">fp</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">SEEK_SET</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">readerr</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">rioInitWithFile</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">fp</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">rdbLoadRio</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">rdb</span><span style="color:#000;font-weight:bold">,</span><span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">C_OK</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">serverLog</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LL_WARNING</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Error reading the RDB preamble of the AOF file, AOF loading aborted</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">goto</span> <span style="color:#000">readerr</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">serverLog</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LL_NOTICE</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Reading the remaining AOF tail...</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span>
<span style="color:#000;font-weight:bold">}</span>
</code></pre></div>
<p>打开AOF文件之后首先读取5个字符，如果是&rdquo;REDIS&rdquo;，那么就说明这是一个混合持久化的AOF文件，正确的RDB格式一定是以&rdquo;REDIS&rdquo;开头，而纯AOF格式则一定以&rdquo;*&ldquo;开头，此时就会进入rdbLoadRio函数来加载数据。</p>

<p>rdbLoadRio函数此处就不详细展开了，就是以约定好的协议解析文件内容，直至遇到RDB_OPCODE_EOF结束标记，返回loadAppendOnlyFile函数继续以AOF格式解析文件直到结束整个加载过程完成。</p>

<h2 id="附录">附录</h2>

<h3 id="1-rdb格式的文件">1. RDB格式的文件</h3>

<p>我们先向redis写入一些数据并生成RDB文件</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-SHELL" data-lang="SHELL"><span style="color:#000">$redis</span>-cli
127.0.0.1:6379&gt; <span style="color:#204a87">set</span> foo bar
OK
127.0.0.1:6379&gt; expire foo <span style="color:#0000cf;font-weight:bold">60</span>
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">1</span>
127.0.0.1:6379&gt; <span style="color:#204a87;font-weight:bold">select</span> <span style="color:#0000cf;font-weight:bold">2</span>
OK
127.0.0.1:6379<span style="color:#ce5c00;font-weight:bold">[</span>2<span style="color:#ce5c00;font-weight:bold">]</span>&gt; <span style="color:#204a87">set</span> foo bar
OK
127.0.0.1:6379<span style="color:#ce5c00;font-weight:bold">[</span>2<span style="color:#ce5c00;font-weight:bold">]</span>&gt; bgsave
Background saving started</code></pre></div>
<p>看下RDB文件内容</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-SHELL" data-lang="SHELL"><span style="color:#000">$cat</span> dump.rdb
REDIS0008    redis-ver4.0.1
redis-bits@ctimeYused-mem 
                                aof-preamblerepl-id<span style="color:#ce5c00;font-weight:bold">(</span>484f9d49a700c4b9b136f0fd40d2d6e5a8460438
                                                                                               repl-offa<span style="color:#000;font-weight:bold">;</span>^foobarfoobar^KJ_U</code></pre></div>
<p>OMG&hellip;一堆乱码隐约可以看到一些和redis相关的字符串为了更直观的感受下RDB的内容我们用redis自带的工具redis-check-rdb来看下</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-SHELL" data-lang="SHELL">redis-check-rdb dump.rdb
<span style="color:#ce5c00;font-weight:bold">[</span>offset 0<span style="color:#ce5c00;font-weight:bold">]</span> Checking RDB file dump.rdb
<span style="color:#ce5c00;font-weight:bold">[</span>offset 26<span style="color:#ce5c00;font-weight:bold">]</span> AUX FIELD redis-ver <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#39;4.0.1&#39;</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 40<span style="color:#ce5c00;font-weight:bold">]</span> AUX FIELD redis-bits <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#39;64&#39;</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 52<span style="color:#ce5c00;font-weight:bold">]</span> AUX FIELD <span style="color:#000">ctime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#39;1504234774&#39;</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 67<span style="color:#ce5c00;font-weight:bold">]</span> AUX FIELD used-mem <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#39;2139016&#39;</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 83<span style="color:#ce5c00;font-weight:bold">]</span> AUX FIELD aof-preamble <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#39;0&#39;</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 133<span style="color:#ce5c00;font-weight:bold">]</span> AUX FIELD repl-id <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#39;484f9d49a700c4b9b136f0fd40d2d6e5a8460438&#39;</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 148<span style="color:#ce5c00;font-weight:bold">]</span> AUX FIELD repl-offset <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#39;0&#39;</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 150<span style="color:#ce5c00;font-weight:bold">]</span> Selecting DB ID <span style="color:#0000cf;font-weight:bold">0</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 173<span style="color:#ce5c00;font-weight:bold">]</span> Selecting DB ID <span style="color:#0000cf;font-weight:bold">2</span>
<span style="color:#ce5c00;font-weight:bold">[</span>offset 194<span style="color:#ce5c00;font-weight:bold">]</span> Checksum OK
<span style="color:#ce5c00;font-weight:bold">[</span>offset 194<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#4e9a06">\o</span>/ RDB looks OK! <span style="color:#4e9a06">\o</span>/
<span style="color:#ce5c00;font-weight:bold">[</span>info<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#0000cf;font-weight:bold">2</span> keys <span style="color:#204a87">read</span>
<span style="color:#ce5c00;font-weight:bold">[</span>info<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#0000cf;font-weight:bold">1</span> expires
<span style="color:#ce5c00;font-weight:bold">[</span>info<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#0000cf;font-weight:bold">0</span> already expired</code></pre></div>
<p>这下就好看多了,首先可以看到是一些AUX FIELD辅助域，4.0特有用来配合全新的主从同步方式PSYNC2，后面会专门来介绍PSYNC2，然后可以看到DB0和DB2是有内容的，Checksum也OK。最后是说一共有2个key，其中一个设置了过期时间到目前为止还都没有过期。</p>

<h3 id="2-aof格式的文件">2. AOF格式的文件</h3>

<p>同样的数据我们来看下在AOF文件中是如何保存的</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">$cat appendonly.aof
*2
$6
SELECT
$1
0
*3
$3
set
$3
foo
$3
bar
*3
$9
PEXPIREAT
$3
foo
$13
1504255377087
*2
$6
SELECT
$1
2
*3
$3
set
$3
foo
$3
bar</code></pre></div>
<p>很明显就是一条一条redis命令。</p>

<h3 id="3-rdb-aof混和持久化的文件">3. RDB-AOF混和持久化的文件</h3>

<p>最后来看下RDB-AOF混和持久化的文件。</p>

<p>首先打开混合开关执行BGREWRITEAOF生成RDB-AOF混合文件，再追加写入一些数据</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#000">$redis</span>-cli
127.0.0.1:6379&gt; config <span style="color:#204a87">set</span> aof-use-rdb-preamble yes
OK
127.0.0.1:6379&gt; BGREWRITEAOF
Background append only file rewriting started
127.0.0.1:6379&gt; <span style="color:#204a87">set</span> foo bar
OK
127.0.0.1:6379&gt; quit</code></pre></div>
<p>再来看下此时AOF文件内容</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">$cat appendonly.aof
REDIS0008    redis-ver4.0.1
redis-bits@ctimeYused-memP 
                                  aof-preamblerepl-id(484f9d49a700c4b9b136f0fd40d2d6e5a8460438
                                                                                                 repl-offsetfoobar?I    Y*2
$6
SELECT
$1
0
*3
$3
set
$3
foo
$3
bar</code></pre></div>
<p>显而易见前半段是RDB格式的全量数据后半段是redis命令格式的增量数据。</p>

<h2 id="优缺点">优缺点</h2>

<h3 id="rdb"> RDB</h3>

<ul>
<li><p>优点：</p>

<ul>
<li>RDB 是一个非常紧凑（compact）的文件，体积小，因此在传输速度上比较快，因此适合灾难恢复。 </li>
<li>RDB 可以最大化Redis 的性能：父进程在保存RDB 文件时唯一要做的就是fork出一个子进程，然后这个子进程就会处理接下来的所有保存工作，父进程无须执行任何磁盘I/O 操作。</li>
<li>RDB 在恢复大数据集时的速度比AOF 的恢复速度要快。</li>
</ul></li>

<li><p>缺点：</p>

<p>RDB是一个快照过程，无法完整的保存所有数据，尤其在数据量比较大时候，一旦出现故障丢失的数据将更多。</p>

<p>当redis中数据集比较大时候，RDB由于RDB方式需要对数据进行完成拷贝并生成快照文件，fork的子进程会耗CPU，并且数据越大，RDB快照生成会越耗时。</p>

<p>RDB文件是特定的格式，阅读性差，由于格式固定，可能存在不兼容情况。</p></li>
</ul>

<h3 id="aof">AOF　</h3>

<ul>
<li><p>优点：</p>

<ul>
<li>数据更完整，秒级数据丢失(取决于设置fsync策略)。</li>
<li>兼容性较高，由于是基于redis通讯协议而形成的命令追加方式，无论何种版本的redis都兼容，再者aof文件是明文的，可阅读性较好。</li>
</ul></li>

<li><p>缺点：</p>

<ul>
<li>数据文件体积较大，即使有重写机制，但是在相同的数据集情况下，AOF文件通常比RDB文件大。</li>
<li>相对RDB方式，AOF速度慢于RDB，并且在数据量大时候，恢复速度AOF速度也是慢于RDB。</li>
<li>由于频繁地将命令同步到文件中，AOF持久化对性能的影响相对RDB较大。</li>
</ul></li>
</ul>

<h3 id="混合持久化">混合持久化</h3>

<ul>
<li><p>优点：</p>

<p>混合持久化结合了RDB持久化 和 AOF 持久化的优点, 由于绝大部分都是RDB格式，加载速度快，同时结合AOF，增量的数据以AOF方式保存了，减少数据丢失。</p></li>

<li><p>缺点：</p>

<p>兼容性差，一旦开启了混合持久化，在4.0之前版本都不识别该aof文件，同时由于前部分是RDB格式，阅读性较差。</p></li>
</ul>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%BA%94-%E5%9F%BA%E4%BA%8Elfu%E7%9A%84%E7%83%AD%E7%82%B9key%E5%8F%91%E7%8E%B0%E6%9C%BA%E5%88%B6/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7-memory%E5%91%BD%E4%BB%A4/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>